//
//  BCPayPasswordInputController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/11.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCPayPasswordInputController.h"

#import "BCPayPassOverlayView.h"

#import "BCWalletPassSetController.h"//重置支付密码
#import "BCUserSafeCenterResponse.h"

static CGFloat BCPayPasswordInputControllerTopPadding = 75.0;
static CGFloat BCPayPasswordInputControllerTFHeight = 50.0;
static CGFloat BCPayPasswordInputControllerBottomMinPadding = 75.0;

@interface BCPayPasswordInputController () <QMUITextFieldDelegate,BCPayPasswordTextFeildDelegate>

/**
 *BCPayPassOverlayView
 */
@property (nonatomic, strong) BCPayPassOverlayView *passwordOverlayView;

/**
 *loadingFlag
 */
@property (nonatomic, assign) BOOL loadingFlag;

@end

@implementation BCPayPasswordInputController

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    [self setupInputSubviews];
    //
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [self.passwordTF becomeFirstResponder];
//    });
}
- (CGFloat)contentHeight {
    return BCPayPasswordInputControllerTopPadding+BCPayPasswordInputControllerTFHeight+BCPayPasswordInputControllerBottomMinPadding;
}
- (void)setupInputSubviews {
    [self.view addSubview:self.backgroundView];
    [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    //
    CGFloat contentHeight = [self contentHeight];
    [self.view addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view);
        make.width.mas_equalTo(self.view);
        make.height.mas_equalTo(contentHeight);
        make.bottom.mas_equalTo(self.view);
    }];
    //
    [self setupContents];
    [self.view layoutIfNeeded];
}
- (void)setupContents {
    self.topView = [UIView new];
    [self.contentView addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self.contentView);
        make.height.mas_equalTo(60.0);
    }];
    //
    self.backButton = [QMUIButton new];
    [self.backButton setImage:[LOAD_IMAGE(@"navi_arrow_left") imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    self.backButton.tintColor = [UIColor qmui_colorWithHexString:@"#222222"];
    [self.topView addSubview:self.backButton];
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.topView).offset(5.0);
        make.centerY.mas_equalTo(self.topView);
    }];
    
    [self.backButton addTarget:self action:@selector(backButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    //
    self.titleLabel = [QMUILabel new];
    self.titleLabel.textColor = [UIColor qmui_colorWithHexString:@"#FF666666"];
    self.titleLabel.numberOfLines = 2;
    self.titleLabel.adjustsFontSizeToFitWidth = true;
    self.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.topView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.topView);
        make.centerY.mas_equalTo(self.topView);
        make.height.mas_equalTo(self.topView);
        make.width.mas_lessThanOrEqualTo(self.topView).offset(-200.0);
    }];
    //
    self.titleLabel.text = BCLocalizedKey(@"Please input pay password");
    //
    self.forgotButton = [QMUILinkButton new];
    [self.forgotButton setTitleColor:[UIColor qmui_colorWithHexString:@"#666666"] forState:UIControlStateNormal];
    self.forgotButton.titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    [self.topView addSubview:self.forgotButton];
    [self.forgotButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.topView).offset(-10.0);
        make.width.mas_lessThanOrEqualTo(88.0);
        make.height.mas_equalTo(35.0);
        make.centerY.mas_equalTo(self.topView);
    }];
    [self.forgotButton setTitle:BCLocalizedKey(@"Forgot password") forState:UIControlStateNormal];
    [self.forgotButton addTarget:self action:@selector(forgotButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    
    //
    self.passwordTF = [BCPayPasswordTextFeild new];
//    self.passwordTF.backgroundColor = [UIColor orangeColor];
    self.passwordTF.textColor = self.contentView.backgroundColor;
    self.passwordTF.tintColor = self.passwordTF.textColor;
    self.passwordTF.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
//    self.passwordTF.secureTextEntry = true;
    self.passwordTF.keyboardType = UIKeyboardTypeNumberPad;
    self.passwordTF.maximumTextLength = 6;//6位数密码
    self.passwordTF.completeDelegate = self;
    [self.contentView addSubview:self.passwordTF];
    [self.passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(BCPayPasswordInputControllerTopPadding);
        make.centerX.mas_equalTo(self.contentView);
        make.width.mas_equalTo(self.contentView).offset(-40.0);
        make.height.mas_equalTo(BCPayPasswordInputControllerTFHeight);
    }];
    self.passwordTF.inputAccessoryView = [UIView new];
    self.passwordTF.enablesReturnKeyAutomatically = true;
    self.passwordTF.returnKeyType = UIReturnKeyDone;
    self.passwordTF.alpha = 0;
    self.passwordTF.delegate = self;
    //
    self.passwordOverlayView = [BCPayPassOverlayView new];
    [self.contentView addSubview:self.passwordOverlayView];
    [self.passwordOverlayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.passwordTF);
    }];
    self.passwordOverlayView.textField = self.passwordTF;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = false;
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.passwordTF becomeFirstResponder];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.view.qmui_top==0) {
            [self.passwordTF resignFirstResponder];
            [self.passwordTF becomeFirstResponder];
        }
    });
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = true;
}


#pragma mark - QMUITextFieldDelegate
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//    if (self.loadingFlag) {
//        return false;
//    }
//    //如果在加载中 false
//    if (self.view.qmui_top==0) {
//        [self.passwordTF resignFirstResponder];
//        [self.passwordTF becomeFirstResponder];
//    }
//
//    if (range.location==5&&range.length==0) {
//        self.loadingFlag = true;
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [self passwordInputFinishAction];
//        });
//    }
//    return true;
//}
#pragma mark - BCPayPasswordTextFeildDelegate
- (void)payPasswordTextFeildInputCompleteAction {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self passwordInputFinishAction];
    });
}


- (void)passwordInputFinishAction {
    
}
//
- (void)startLoading {
    self.loadingFlag = true;
    self.topView.alpha = 0.5;
    self.topView.userInteractionEnabled = false;
    [self.passwordOverlayView bc_showActivityIndicatorBackgroundColor:self.contentView.backgroundColor insets:UIEdgeInsetsZero];
}
- (void)stopLoading {
    self.loadingFlag = false;
    [self.passwordOverlayView bc_hiddenActivityIndicator];
    self.topView.alpha = 1;
    self.topView.userInteractionEnabled = true;
}

#pragma mark - forgotButtonDidClickedAction
/**
 忘记密码
 */
- (void)forgotButtonDidClickedAction {
    //需要处理数据
    BCUserSafeCenterData *data = [BCUserManager sharedUser].safeCenterData;
    NSString *account;
    BCaptchaSendType sendType = BCaptchaSendTypePhone;
    if (data.data.has_email) {
        account = data.data.email;
        sendType = BCaptchaSendTypeEmail;
    }else if (data.data.has_phone) {
        account = data.data.phone;
        sendType = BCaptchaSendTypePhone;
    }
    if (!account.length) {
        return;
    }
    BCWalletPassSetController *pass = [BCWalletPassSetController new];
    pass.account = account;
    pass.sendType = sendType;
    [self.navigationController pushViewController:pass animated:true];
}

#pragma mark - backButtonDidClickedAction
- (void)backButtonDidClickedAction {
    [self dismissWindow];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    if (self.view.qmui_top==0) {
        [self.passwordTF resignFirstResponder];
        [self.passwordTF becomeFirstResponder];
    }
}


- (void)dealloc {
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = true;
}

@end
